// pages/login/forgotPassword/index.js
import { SendSmsCode, ResetPass } from '../../../services/auth/auth';
import util from "../../../utils/util.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      telephone: '15010123237',
      password: '',
      smsCode: '',
    },
    rules: {
      telephoneError: false,
      telephoneMessage: '手机号输入不正确',
      passwordError: false,
      passwordMessage: "密码至少六位数字",
      smsCodeError: false,
      smsCodeMessage: '请输入验证码'
    },
    codeLoading: false,   // 发送验证码按钮loading
    countdownTime: 60,    // 验证码倒计时时间
    countdownTimer: null, // 验证码倒计时定时器
    loading: false,   // 提交loading
  },

  /* 发送验证码 */
  sendSmsCode() {
    if (this.data.form.telephone == '') {
      util.showToast('请输入手机号', "error");
      return;
    }
    this.setData({ codeLoading: true })
    SendSmsCode({ telephone: this.data.form.telephone }).then(res => {
      if (res.code == '0000') {
        util.showToast('短信验证码发送成功, 请注意查收!', "none");
        this.setData({ codeLoading: false })
        this.startCountdownTimer();
      }
    })
  },
  /* 监听表单 */
  onInput(event) {
    const { key } = event.currentTarget.dataset;
    const { value } = event.detail;
    this.setData({ ['form.' + [key]]: value });
    switch (key) {
      case 'telephone':
        this.setData({ ['rules.' + [key] + 'Error']: !util.phoneRegCheck(value) });
        break;
      case 'password':
        const inputRegExp = /^\d{6,18}$/;
        this.setData({ ['rules.' + [key] + 'Error']: !inputRegExp.test(value) });
        break;
      case 'smsCode':
        if (value == '') this.setData({ ['rules.' + [key] + 'Error']: true });
        else this.setData({ ['rules.' + [key] + 'Error']: false });
        break;
      default:
        break;
    };
    // this.setData({ ['rules.' + [key] + 'Error']: !inputRegExp.test(value) });
  },
  /* 提交 */
  submit() {
    if (this.data.form.telephone == '') this.setData({ ['rules.telephoneError']: true });
    if (this.data.form.password == '') this.setData({ ['rules.passwordError']: true });
    if (this.data.form.smsCode == '') this.setData({ ['rules.smsCodeError']: true });
    if (this.data.rules.telephoneError || this.data.rules.smsCodeError || this.data.rules.passwordError) return false;
    this.setData({ loading: true })
    ResetPass(this.data.form).then(res => {
      this.setData({ loading: false })
      if (res.code == '0000') {
        util.showToast('修改成功', "success", 600);
        wx.navigateBack({
          delta: 1
        });
      }
    })
  },
  /* 开始对按钮进行倒计时 */
  startCountdownTimer() {
    let countdownTime = this.data.countdownTime, that = this;
    this.setData({
      countdownTimer: setInterval(() => {
        if (that.data.countdownTime <= 1) {
          clearInterval(that.data.countdownTimer);
          that.setData({ countdownTimer: null })
        }
        countdownTime--;
        that.setData({ countdownTime })
      }, 1000)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (this.data.countdownTime) clearInterval(this.data.countdownTimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.countdownTime) clearInterval(this.data.countdownTimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})